var express = require("express");
console.log(express);

var router = express.Router();
var db = require("../database/DB");
const { data } = require("jquery");
const app = express()
//中间件
app.use((req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*"); // 或者指定特定的域名
    res.header("Access-Control-Allow-Headers", "Content-Type");
    next();
});
// 获取
app.get('/users', function (req, res) {
    let sql = "SELECT * FROM houser_list"
    db.query(sql, (err, result) => {
        if (err) {
            res.json({
                code: 400,
                msg: "响应失败",
            })
        } else {
            res.json({
                code: 200,
                msg: "响应成功",
                data: result,
            })
        }
    })
})
//添加
// 添加
app.post("/add", function (req, res) {
    let sql = `INSERT INTO houser_list(title) VALUES (?)`;
    db.query(sql, [req.body.title], (err, data) => {
        if (err) {
            console.log(err);
            res.json({
                code: 1,
                msg: "添加失败",
            });
        } else {
            res.json({
                code: 0,
                msg: "添加成功",
            });
        }
    });
});

//删除
app.get("/del", (req, res) => {
    let sql = `DELETE FROM houser_list WHERE house_id = ${req.query.house_id}`;
    db.query(sql, (err, data) => {
        if (err) {
            res.json({
                code: 1,
                msg: "删除失败",
            });
        } else {
            res.json({
                code: 0,
                msg: "删除成功",
            });
        }
    });
});
app.listen(3000, () => {
    console.log("启动服务；http://127.0.0.1:3000");
})